package com.wecloud.core.data.jnlservice;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.wecloud.core.constant.WeContext;
import com.wecloud.core.data.jnl.JnlService;
import com.wecloud.core.exception.WeException;
import com.wecloud.core.utils.GenarateSeqUtil;

public class UploadJnlService extends JnlService{
	
	private String savefile;
	
	public String getSavefile() {
		return savefile;
	}

	public void setSavefile(String savefile) {
		this.savefile = savefile;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void write(Map map) throws WeException {
		//保存
		getTransactionTemplate().execute(new TransactionCallback() {
			@Override
			public Object doInTransaction(TransactionStatus status) {
				try{
					List<Map> list = (List)map.get("List");
					if(list != null && list.size() > 0){
						for(Map o : list){
							o.put("fid", GenarateSeqUtil.genarateUUID());
							o.put("userid", map.get("userid"));
							o.put("time", new Date());
							insert(savefile, o);
						}
					}
				}catch (Exception  e) {
					getAnnotationDrivenTransactionManager().rollback(status);
					throw new  WeException(e);
				}
				return null;
			}
		});
	}
	@Override
	public void prepareJnlData(WeContext context) {
		context.setData("userid", context.getUser().getUserId());
	}
}
